package main

import (
	"ginweb/DB_operation"
	"ginweb/config"
	"ginweb/router"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	//核心对象	gin.Engine
	g := gin.Default()
	//读取配置文件
	//实例化一个config的结构体
	conf := config.Config{}
	if err := conf.InitConfig(); err != nil {
		//fmt.Println("------------------------------------------------")
		panic(err) //读取配置文件失败，直接抛出异常，终止程序运行
	}
	// 初始化数据库
	DB_operation.InitDB()
	defer DB_operation.Close()
	//
	// 允许跨域
	g.Use(func(c *gin.Context) {
		c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
		c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
		c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept, Authorization,X-CSRF-Token")
		c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length")
		c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(200)
		} else {
			c.Next()
		}
	})
	// 设置静态文件目录
	g.Static("/static", "./static")
	//配置html文件位置

	//g.LoadHTMLGlob("web\\admin\\*")
	//g.LoadHTMLGlob("web\\login\\*")
	//g.LoadHTMLGlob("web/*")

	//连接数据库
	//init.OpenDB()
	//db := OpenDB()
	//路由

	//配置端口
	router.Load(g)
	g.Run(":9999")
}
